package com.dingreading.cloud.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingreading.cloud.app.entity.Stores;
import com.dingreading.cloud.app.entity.table.StoresTableDef;
import com.dingreading.cloud.app.mapper.StoresMapper;
import com.dingreading.cloud.app.service.StoresService;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class StoresServiceImpl extends BaseServiceImpl<StoresMapper, Stores, StoresTableDef> implements StoresService {


    @Override
    protected StoresTableDef getTable() {
        return StoresTableDef.stores;
    }

    @Override
    public PageDto<JSONObject> pageList(PageUtil pageUtil, String corpUid, String keyword, Integer operateStatus, Integer enabled) {
        //设置其他参数
        Map<String, Object> otherParams = new HashMap<>();
        otherParams.put("corpUid", corpUid);
        otherParams.put("keyword", keyword);
        otherParams.put("operateStatus", operateStatus);
        otherParams.put("enabled", enabled);

        Page<JSONObject> page = getMapper().xmlPaginate("pageListXml", Page.of(pageUtil.getPage(), pageUtil.getRows()), otherParams);

        long totalRow = page.getTotalRow();
        List<JSONObject> list = page.getRecords();

        try {
            if (CollectionUtils.isNotEmpty(list)) {
                for (JSONObject jsonObject : list) {
                    String addTimeStr = "";
                    Date addTime = jsonObject.getDate("addTime");
                    if (addTime != null) {
                        addTimeStr = DateUtil.getDefaultTimeStr(addTime);
                    }
                    jsonObject.put("addTime", addTimeStr);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 结果
        PageDto<JSONObject> dto = new PageDto<>();
        dto.setList(list);
        dto.setTotal(totalRow);

        return dto;
    }

    @Override
    public Stores getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public List<Stores> getByUids(List<String> uids) {
        return getMapper().selectListByCondition(table.uid.in(uids));
    }

    @Override
    public List<Stores> getByCorpUid(String corpUid, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.corpUid.eq(corpUid));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public List<Stores> getByCorpUidAndEnabled(String corpUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.corpUid.eq(corpUid))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.uid, table.storeName, table.corpUid)
                .from(table)
                .where(condition);

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<Stores> getCorpOtherStores(String corpUid, String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.corpUid.eq(corpUid))
                .and(table.uid.ne(storeUid))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.uid, table.storeName, table.corpUid)
                .from(table)
                .where(condition);

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public boolean updateLatAndLng(String storeUid, String latitude, String longitude) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(storeUid));

        return UpdateChain.of(Stores.class)
                .set(table.lat, latitude)
                .set(table.lng, longitude)
                .where(condition)
                .update();
    }

    @Override
    public List<Stores> getByLatAndLngNotUid(String storeUid, String latitudeMin, String latitudeMax, String longitudeMin, String longitudeMax) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.uid.ne(storeUid));
        condition.and(table.lng.ne("").and(table.lng.isNotNull()))
                .and(table.lat.ne("").and(table.lat.isNotNull()))
                .and(table.lng.between(longitudeMin, longitudeMax))
                .and(table.lat.between(latitudeMin, latitudeMax));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.corpUid, table.uid, table.storeName, table.lng, table.lat)
                .from(table)
                .where(condition);

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<Stores> getStoreMarker() {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.lng.ne(""))
                .and(table.lat.ne(""))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.corpUid, table.uid, table.storeName, table.lng, table.lat)
                .from(table)
                .where(condition);

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<Stores> getByKeywordAndEnable(String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.storeName.like(keyword)
                    .or(table.address.like(keyword)));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public boolean updateOperateInfo(String storeUid, String operateStaffUid, String operateStaffName, String operateStaffPhone) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(storeUid));

        return UpdateChain.of(Stores.class)
                .set(table.operateStaffUid, operateStaffUid)
                .set(table.operateStaffName, operateStaffName)
                .set(table.operateStaffPhone, operateStaffPhone)
                .where(condition)
                .update();
    }

    @Override
    public Stores getByCorpUidAndStoreUid(String corpUid, String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(corpUid))
            condition.and(table.corpUid.eq(corpUid));
        condition.and(table.uid.eq(storeUid))
                .and(table.enabled.eq(1));
        return getMapper().selectOneByCondition(condition);
    }
}
